#include <algorithm>
#include <iostream>
#include <climits>
#include <vector>
#include <cmath>
using namespace std;

void get_divisor(int n, vector<int>& divisor)
{
    for(int i = 2; i <= sqrt(n); i++)
    {
        if(n % i == 0)
        {
            divisor.push_back(i);
            if(n / i != i)
            {
                divisor.push_back(n / i);
            }
        }
    }
}

int JumpStone(int n, int m)
{
    vector<int> step(m + 1, INT_MAX);
    step[n] = 0;

    for(int i = n; i < m; i++)
    {
        if(step[i] == INT_MAX)
            continue;
        
        vector<int> divisor;
        get_divisor(i, divisor);

        for(int j = 0; j < divisor.size(); j++)
        {
            if(i + divisor[j] <= m && step[i + divisor[j]] != INT_MAX)
            {
                step[i + divisor[j]] = step[i + divisor[j]] < step[i] + 1 ? step[i + divisor[j]] : step[i] + 1;

            }
            else if(i + divisor[j] <= m)
            {
                step[i + divisor[j]] = step[i] + 1;
            }
        }
    }
    return step[m] == INT_MAX ? -1 : step[m];
}

int main()
{
    int n, m;
    cin >> n >> m;
    int min_step = JumpStone(n, m);
    cout << min_step << endl;
    return 0;
}
